package com.imay.live.activity.family.controller;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.imay.live.R;
import com.imay.live.activity.family.FamilyRankActivity;
import com.imay.live.activity.family.util.LevelUtil;
import com.imay.live.fragment.RankingFragment;
import com.imay.live.fragment.dynamics.LinearFragment;
import com.imay.live.manager.FamilyAccountManager;
import com.imay.live.model.RankingModel;
import com.imay.live.utils.GlideUtil;
import com.imay.live.utils.ViewUtil;
import com.netease.nim.uikit.common.util.sys.ScreenUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/9/15 0015.
 */

public class FamilyRankController extends BaseFamilyController {

    RankingFragment rankingFragment;
    private TextView headView;

    @Override
    protected void initView() {
        if (headView == null) {
            headView = new TextView(activity);
            headView.setGravity(Gravity.CENTER);
            headView.setText(R.string.str_family_rank_note);
            headView.setTextSize(13);
            headView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, ScreenUtil.dip2px(32)));
        }
        rankingFragment = RankingFragment.newInstance(RankingFragment.TYPE_FAMILY);
        addFragment(rankingFragment);

        ViewUtil.findView(activity, R.id.ll_follow).setVisibility(View.INVISIBLE);
    }

    private void addFragment(Fragment loadFragment) {
        FragmentManager fragmentManager = ((FamilyRankActivity) activity).getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Fragment fragment = fragmentManager.findFragmentByTag(LinearFragment.class.toString());
        if (fragment == null) {
            transaction.add(R.id.fl_family_rank_content, loadFragment, LinearFragment.class.toString());
        }
        transaction.show(loadFragment);
        transaction.commitAllowingStateLoss();
    }

    public void setRankData(ArrayList<RankingModel> rankData) {
        rankingFragment.setRankingData(rankData, headView);
    }

    public void setMyRank(RankingModel myRank) {
        if (myRank != null) {
            ImageView iv_hear_img_ranking = ViewUtil.findView(activity, R.id.iv_hear_img_ranking);
            GlideUtil.loadCircleImage(iv_hear_img_ranking, myRank.getUser().getImgHead(), R.mipmap.pnl_00_head108);

            ViewUtil.findView(activity, R.id.tv_anchor_level_ranking).setVisibility(View.GONE);
            ViewUtil.findView(activity, R.id.tv_user_level_ranking).setVisibility(View.GONE);

            TextView tv_name_ranking = ViewUtil.findView(activity, R.id.tv_name_ranking);
            tv_name_ranking.setText(myRank.getUser().getNick());

            TextView tv_anchor_family_ranking = ViewUtil.findView(activity, R.id.tv_anchor_family_ranking);
            tv_anchor_family_ranking.setTextColor(activity.getResources().getColor(LevelUtil.getLevelColor(myRank.getUser().getLv())));
            if (FamilyAccountManager.getInstance().getFamilyExps() != null
                    && FamilyAccountManager.getInstance().getFamilyUserExps().containsKey(myRank.getUser().getLv())) {
                tv_anchor_family_ranking.setText(FamilyAccountManager.getInstance().getFamilyUserExps().get(myRank.getUser().getLv()).getTitle());
            }

            TextView tv_value_ranking = ViewUtil.findView(activity, R.id.tv_value_ranking);
            tv_value_ranking.setText(myRank.getValue() + activity.getString(R.string.gongxian));

            /*TextView tv_family_my_rank = ViewUtil.findView(activity, R.id.tv_family_my_rank);
            if (myRank.getRank() == -1) {
                tv_family_my_rank.setText(R.string.str_family_rank_no_in);
            } else {
                tv_family_my_rank.setText(String.valueOf(myRank.getRank() + 1));
            }*/
            TextView tv_ranking_rank = ViewUtil.findView(activity, R.id.tv_ranking_rank);
            if (myRank.getRank() == -1) {
                tv_ranking_rank.setText(R.string.str_family_rank_no_in);
            } else {
                tv_ranking_rank.setText(String.format(activity.getString(R.string.ranking_rank), myRank.getRank() + 1));
            }
        }
    }
}
